﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.Web;
using ESF.General;
using ESF.General.Entity;

#endregion


public partial class _Default : ESF.Web.Pages.BasePage
{
    #region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
    {
		ScriptManager.GetCurrent (this).RegisterAsyncPostBackControl (dpgErrors);

		if (!IsPostBack)
		{
			lblTitle.Text = Title;

			Clear ();
		}
    }

	#endregion


	#region Funciones

	private void Browse ()
	{
		try
		{
			btnDelete.Enabled = GridLoader.LoadGrid<ErrorReport> (grvErrors, dpgErrors,
				AdoGeneral.GetErrors (
					new ErrorReport () { 
						ApplicationName = ddlApplicationName.SelectedValue,
						UserName = txtUserName.Text,
						filStartErrorLogDate = TextHelper.GetNullableDate (txtStartLogDate.Text),
						filEndErrorLogDate = TextHelper.GetNullableDate (txtEndLogDate.Text),
						Message = ddlMessage.SelectedValue,
						filOrderBy = ddlOrderBy.SelectedValue,
						DetailMessage = ddlDetailMessage.SelectedValue,
						ExceptionType = ddlExceptionType.SelectedValue}, 
					dpgErrors.PaggingInfo));
		}
		catch (Exception pEx)
		{
			JS.LogError (Page, pEx);
		}
	}


	private void ShowLists ()
	{
		ErrorReport filter;


		GridLoader.Clear(grvErrors, dpgErrors);

		if (ddlApplicationName.SelectedValue.Length == 0)
		{
			ListLoader.Clear(ddlDetailMessage);
			ListLoader.Clear(ddlExceptionType);
			ListLoader.Clear(ddlMessage);
		}
		else
		{
			filter = new ErrorReport()
			{
				ApplicationName = ddlApplicationName.SelectedValue,
				filStartErrorLogDate = TextHelper.GetNullableDate(txtStartLogDate.Text),
				filEndErrorLogDate = TextHelper.GetNullableDate(txtEndLogDate.Text),
				filOrderBy = ddlOrderBy.SelectedValue,
				CustomUserName = txtUserName.Text,
				UserName = txtUserName.Text
			};

			ListLoader.LoadList(ddlDetailMessage, AdjustListItems(AdoGeneral.ListDetailMessage(filter)), "Name", "Extra_I");
			ListLoader.LoadList(ddlExceptionType, AdjustListItems(AdoGeneral.ListExceptionType(filter)), "Name", "Extra_I");
			ListLoader.LoadList(ddlMessage, AdjustListItems(AdoGeneral.ListMessage(filter)), "Name", "Extra_I");
		}
	}


	private void Clear ()
	{
		txtStartLogDate.Text = DateTime.Now.AddDays (-7).ToString (txtStartLogDate_CalendarExtender.Format);
		txtEndLogDate.Text = DateTime.Now.ToString(txtStartLogDate_CalendarExtender.Format);

		pnlExtraFilters.Visible = false;

		dpgErrors.Visible = false;
		grvErrors.Visible = false;
	}


	private void FirstFilter ()
	{
		ErrorReport filter;

		filter = new ErrorReport ()
		{
			filStartErrorLogDate = TextHelper.GetNullableDate (txtStartLogDate.Text),
			filEndErrorLogDate = TextHelper.GetNullableDate (txtEndLogDate.Text),
			filOrderBy = ddlOrderBy.SelectedValue,
			CustomUserName = txtUserName.Text,
			UserName = txtUserName.Text
		};


		ListLoader.LoadList(ddlApplicationName, AdjustListItems (AdoGeneral.ListApplicationName (filter)), "Name", "Extra_I");

		ListLoader.Clear(ddlDetailMessage);
		ListLoader.Clear(ddlExceptionType);
		ListLoader.Clear(ddlMessage);

		txtUserName.Text = string.Empty;

		pnlExtraFilters.Visible = true;
	}


	private List<BasicReference> AdjustListItems (IEnumerable<BasicReference> pItems)
	{
		List<BasicReference> result = new List<BasicReference> ();


		foreach (BasicReference item in pItems)
		{
			item.Extra_I = string.Format("({0}) {1}", item.Extra_I, item.Name);

			result.Add (item);
		}

		return result;
	}

	#endregion


	#region Eventos de los Botones

	protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
	{
		GridLoader.CheckHeaderAll (grvErrors, "chkSelectAll", "chkSelect");
	}


	protected void btnEmptyLog_Click(object sender, EventArgs e)
	{
		try
		{
			AdoGeneral.EmptyLog (ddlApplicationName.SelectedValue);
			
			Clear ();
		}
		catch (Exception pEx)
		{
			JS.LogError (Page, pEx);
		}
	}


	protected void btnClearUntilLastSevenDays_Click(object sender, EventArgs e)
	{
		try
		{
			AdoGeneral.ClearUntilLastSevenDays ();

			Clear ();
		}
		catch (Exception pEx)
		{
			Log.Error (pEx);
		}
	}


	protected void btnDelete_Click(object sender, EventArgs e)
	{
		IEnumerable <int> selectedIDs;


		try
		{
			selectedIDs = GridLoader.GetSelectedIdentifiersAsInt (grvErrors);

			if (DataHelper.IsEmpty (selectedIDs))
			{
				JS.AddWarning (Page, "Seleccione al menos un elemento para eliminarlo");
				return;
			}

			AdoGeneral.Delete (selectedIDs.ToArray ());
			
			Browse ();
		}
		catch (Exception pEx)
		{
			JS.LogError (Page, pEx);
		}
	}


	protected void btnBrowse_Click(object sender, EventArgs e)
	{
		Browse ();
	}
	
	protected void btnClear_Click(object sender, EventArgs e)
	{
		Clear ();
	}

	protected void btnFirstFilter_Click(object sender, EventArgs e)
	{
		FirstFilter();
	}

	#endregion


	#region Eventos de la Grilla

	protected void grvErrors_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		ErrorReport item;


		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			item = e.Row.DataItem as ErrorReport;

			GridLoader.SetValueIDForRow (e.Row, item.ID);

			(e.Row.FindControl ("lblApplicationName") as Label).Text = item.ApplicationName;
			(e.Row.FindControl ("lblCode") as Label).Text = item.Code;
			(e.Row.FindControl ("lblUserName") as Label).Text = item.UserName;
			(e.Row.FindControl ("lblDomainName") as Label).Text = item.DomainName;
			(e.Row.FindControl ("lblMachineAddress") as Label).Text = item.MachineAddress;
			(e.Row.FindControl ("lblRemoteMachineAddress") as Label).Text = item.RemoteMachineAddress;
			(e.Row.FindControl ("lblMachineName") as Label).Text = item.MachineName;
			(e.Row.FindControl ("lblErrogLogDate") as Label).Text = item.ErrorLogDate.ToString ();
			(e.Row.FindControl ("lblOSVersion") as Label).Text = item.OSVersion;
			(e.Row.FindControl ("lblRuntimeVersion") as Label).Text = item.RuntimeVersion;
			(e.Row.FindControl("lblWebProxyAddressPort") as Label).Text = item.WebProxyAddressPort;
			
			(e.Row.FindControl ("txtMessage") as TextBox).Text = item.Message;
			(e.Row.FindControl ("txtDetail") as TextBox).Text = item.DetailMessage;
			(e.Row.FindControl ("txtStackTrace") as TextBox).Text = item.StackTrace;

			(e.Row.FindControl ("lblErrorCount") as Label).Text = item.calErrorCount.ToString ();

			(e.Row.FindControl("lblCustomUserName") as Label).Text = item.CustomUserName;
			(e.Row.FindControl("lblProductVersion") as Label).Text = item.ProductVersion;

			(e.Row.FindControl("lblExceptionType") as Label).Text = item.ExceptionType;

			(e.Row.FindControl("txtUsersWithSameError") as TextBox).Text = TextHelper.Concatenate (" ,", item.calUsersWithSameError.ToArray ());

			(e.Row.FindControl("txtUsersWithSameErrorAndDate") as TextBox).Text = TextHelper.Concatenate(" ,", item.calUsersWithSameErrorAndDate.ToArray()); 
		}
	}

	#endregion


	#region Eventos de Paginacion

	protected void dpgErrors_OnPageChanged()
	{
		Browse ();
	}

	#endregion
	
	
	#region Excepcion de Prueba

	protected void lnkThrowTestException_Click(object sender, EventArgs e)
	{
		Log.Error (new Exception ("Testing"));
	}

	#endregion
	

	#region DropDownList Events

	protected void ddlApplicationName_SelectedIndexChanged(object sender, EventArgs e)
	{
		ShowLists ();
	}

	#endregion
	
}